﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Entities.Commodities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.IO;
    using Hidistro.UI.SaleSystem.Tags;
    using Hidistro.Core;
    using System.Web.UI.WebControls;
    using System.Text;

    public class detailpages : HtmlTemplatedWebControl
    {
        private int productId;
        private wnxd_DetailPagesLocation DetailPagesLocation;
        private wnxd_ProductImages ProductImages;
        private Literal litProductName;
        private Label lblBuyPrice;
        private FormatedMoneyLabel lblMarkerPrice;
        private Literal lblProductCode;
        private Literal litBrosedNum;
        private Literal litSaleCounts;
        private SKUSelector skuSelector;
        private TotalLabel lblTotalPrice;
        private StockLabel lblStock;
        private Literal litUnit;
        private AddCartButton btnaddgouwu;
        private BuyButton btnBuy;
        private Literal litDescription;
        private HyperLink hpkProductReviews;
        private Common_ProductReview reviews;
        private HyperLink hpkProductSales;
        private HyperLink hpkProductConsultations;
        private wnxd_RelatedProducts RelatedProducts;
        private SkuLabel lblSku;
        private Common_ProductConsultations consultations;
        private wnxd_DigitalDisplay wnxd_DigitalDisplay0;
        private wnxd_DigitalDisplay wnxd_DigitalDisplay1;
        private wnxd_DigitalDisplay wnxd_DigitalDisplay2;
        private wnxd_CaiNiXiHuan wnxd_CaiNiXiHuan;
        private wnxd_Display cnxh_display;
        public string isCaiNi = "style=\"display:none;\"";

        protected override void AttachChildControls()
        {
            if (!int.TryParse(this.Page.Request.QueryString["productId"], out this.productId))
            {
                base.GotoResourceNotFound();
            }
            this.DetailPagesLocation = (wnxd_DetailPagesLocation)this.FindControl("wnxd_DetailPagesLocation");
            this.ProductImages = (wnxd_ProductImages)this.FindControl("wnxd_ProductImages");
            this.litProductName = (Literal)this.FindControl("litProductName");
            this.lblBuyPrice = (Label)this.FindControl("lblBuyPrice");
            this.lblMarkerPrice = (FormatedMoneyLabel)this.FindControl("lblMarkerPrice");
            this.lblProductCode = (Literal)this.FindControl("lblProductCode");
            this.litBrosedNum = (Literal)this.FindControl("litBrosedNum");
            this.litSaleCounts = (Literal)this.FindControl("litSaleCounts");
            this.skuSelector = (SKUSelector)this.FindControl("SKUSelector");
            this.lblTotalPrice = (TotalLabel)this.FindControl("lblTotalPrice");
            this.lblStock = (StockLabel)this.FindControl("lblStock");
            this.litUnit = (Literal)this.FindControl("litUnit");
            this.btnaddgouwu = (AddCartButton)this.FindControl("btnaddgouwu");
            this.btnBuy = (BuyButton)this.FindControl("btnBuy");
            this.litDescription = (Literal)this.FindControl("litDescription");
            this.hpkProductReviews = (HyperLink)this.FindControl("hpkProductReviews");
            this.reviews = (Common_ProductReview)this.FindControl("list_Common_ProductReview");
            this.hpkProductSales = (HyperLink)this.FindControl("hpkProductSales");
            this.hpkProductConsultations = (HyperLink)this.FindControl("hpkProductConsultations");
            this.RelatedProducts = (wnxd_RelatedProducts)this.FindControl("wnxd_RelatedProducts");
            this.lblSku = (SkuLabel)this.FindControl("lblSku");
            this.consultations = (Common_ProductConsultations)this.FindControl("list_Common_ProductConsultations");
            this.wnxd_DigitalDisplay0 = (wnxd_DigitalDisplay)this.FindControl("wnxd_DigitalDisplay0");
            this.wnxd_DigitalDisplay1 = (wnxd_DigitalDisplay)this.FindControl("wnxd_DigitalDisplay1");
            this.wnxd_DigitalDisplay2 = (wnxd_DigitalDisplay)this.FindControl("wnxd_DigitalDisplay2");
            this.wnxd_CaiNiXiHuan = (wnxd_CaiNiXiHuan)this.FindControl("wnxd_CaiNiXiHuan");
            this.cnxh_display = (wnxd_Display)this.FindControl("cnxd_display");

            if (!this.Page.IsPostBack)
            {
                ProductBrowseInfo info = ProductBrowser.GetProductBrowseInfo(this.productId, null, null);
                if (info.Product == null)
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该件商品已经被管理员删除"));
                }
                else
                {
                    if (info.Product.SaleStatus == ProductSaleStatus.UnSale)
                    {
                        this.Page.Response.Redirect(Globals.GetSiteUrls().UrlData.FormatUrl("unproductdetails", new object[] { this.Page.Request.QueryString["productId"] }));
                    }
                    if (info.Product.SaleStatus == ProductSaleStatus.OnStock)
                    {
                        this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该商品已入库"));
                    }
                    else
                    {
                        this.LoadPageSearch(info.Product);
                        this.LoadControls(info.Product, info.BrandName);
                        this.skuSelector.ProductId = this.productId;
                        this.skuSelector.DataSource = info.DbSKUs;
                        this.hpkProductReviews.Text = "查看全部" + info.ReviewCount + "条评论"; this.hpkProductReviews.NavigateUrl = string.Format("LookProductReviews.aspx?productId={0}", this.productId);
                        this.hpkProductReviews.NavigateUrl = string.Format("LookProductReviews.aspx?productId={0}", this.productId);
                        this.reviews.DataSource = info.DBReviews;
                        this.reviews.DataBind();
                        int CJJLCount = ProductBrowser.GetLineItemNumber(this.productId);
                        this.hpkProductSales.Text = "查看全部" + CJJLCount + "条成交记录";
                        this.hpkProductSales.NavigateUrl = string.Format("LookLineItems.aspx?productId={0}", this.productId);
                        this.hpkProductConsultations.Text = "查看全部" + info.ConsultationCount.ToString() + "条咨询";
                        this.hpkProductConsultations.NavigateUrl = string.Format("ProductConsultationsAndReplay.aspx?productId={0}", this.productId);
                        this.RelatedProducts.productId = this.productId;
                        if (info.DBConsultations != null)
                        {
                            this.consultations.DataSource = info.DBConsultations;
                            this.consultations.DataBind();
                        }
                        this.wnxd_DigitalDisplay0.ShowCount = info.ReviewCount;
                        this.wnxd_DigitalDisplay1.ShowCount = CJJLCount;
                        this.wnxd_DigitalDisplay2.ShowCount = info.ConsultationCount;
                        BrowsedProductQueue.EnQueue(this.productId);
                        this.wnxd_CaiNiXiHuan.productId = this.productId;
                        this.wnxd_CaiNiXiHuan.display = this.cnxh_display;
                    }
                }
            }
        }
        private void LoadPageSearch(ProductInfo productDetails)
        {
            if (!string.IsNullOrEmpty(productDetails.MetaKeywords))
            {
                MetaTags.AddMetaKeywords(productDetails.MetaKeywords, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(productDetails.MetaDescription))
            {
                MetaTags.AddMetaDescription(productDetails.MetaDescription, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(productDetails.Title))
            {
                PageTitle.AddSiteNameTitle(productDetails.Title);
            }
            else
            {
                PageTitle.AddSiteNameTitle(productDetails.ProductName);
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-detailpages.html";
            }
            base.OnInit(e);
        }
        private void LoadControls(ProductInfo productDetails, string BrandName)
        {
            this.DetailPagesLocation.CateGoryPath = productDetails.MainCategoryPath.Remove(productDetails.MainCategoryPath.Length - 1);
            this.DetailPagesLocation.ProductName = productDetails.ProductName;
            //this.ProductImages.ImageInfo = productDetails;

            StringBuilder builder = new StringBuilder();
            if (BrandName != null && productDetails.BrandId != null)
            {
                builder.AppendFormat("<a class=\"detail-brand\" href=\"{0}\">{1}</a>&nbsp;", Globals.GetSiteUrls().SubBrandDetails((int)productDetails.BrandId, null), BrandName);
            }
            this.litProductName.Text = builder.ToString() + productDetails.ProductName + " <font color=\"red\">" + productDetails.ProductText + "</font>";
            if (productDetails.MinSalePrice == productDetails.MaxSalePrice)
            {
                this.lblBuyPrice.Text = productDetails.MinSalePrice.ToString("F0");
                this.lblTotalPrice.Value = new decimal?(productDetails.MinSalePrice);
            }
            else
            {
                this.lblBuyPrice.Text = productDetails.MinSalePrice.ToString("F0") + " - " + productDetails.MaxSalePrice.ToString("F0");
            }
            this.lblMarkerPrice.Money = productDetails.MarketPrice;
            this.lblProductCode.Text = productDetails.ProductCode;
            this.litBrosedNum.Text = productDetails.VistiCounts.ToString();
            this.litSaleCounts.Text = productDetails.ShowSaleCounts.ToString();
            this.lblStock.Stock = productDetails.Stock;
            this.litUnit.Text = productDetails.Unit;
            this.btnaddgouwu.Stock = productDetails.Stock;
            this.btnBuy.Stock = productDetails.Stock;
            this.litDescription.Text = Globals.ReplaceImgUrl(productDetails.Description);
            this.lblSku.Text = productDetails.SKU;
            this.lblSku.Value = productDetails.SkuId;
        }
    }
}
